package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;

import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
@RequestMapping("shopping")
public class CartController {

    @Autowired
    ICartService cartService;




    @GetMapping("carts")
    public ResponseData cartList(HttpServletRequest request){





        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        CartListByIdRequest listByIdRequest = new CartListByIdRequest();
        listByIdRequest.setUserId(uid);

        CartListByIdResponse cartListById = cartService.getCartListById(listByIdRequest);
        if (cartListById == null){
            return new ResponseUtil().setErrorMsg("服务器繁忙");
        }

        if (!cartListById.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(cartListById.getMsg());
        }

        return new ResponseUtil<>().setData(cartListById.getCartProductDtos());
    }


    @PostMapping("carts")
    public ResponseData addCart(@RequestBody CartForm form, HttpServletRequest request){
        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        form.setUserId(uid);
        AddCartRequest addCartRequest = new AddCartRequest();

        addCartRequest.setUserId(form.getUserId());
        addCartRequest.setItemId(form.getProductId());
        addCartRequest.setNum(form.getProductNum());

        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);
        if (addCartResponse == null){
            return new ResponseUtil().setErrorMsg("服务器繁忙");
        }
        if (!addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
        }

        return new ResponseUtil<>().setData("成功");
    }


    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody CartForm cartForm, HttpServletRequest request){
        // String token = CookieUtil.getCookieValue(request, "access_token");
        //
        // String uidStr = JwtTokenUtils.builder().token(token).build().freeJwt();
        // Long uid = Long.valueOf(uidStr);

        String userInfo = (String) request.getHeader("user_info");
        System.out.println("!!!!!!!!!" +userInfo);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        cartForm.setUserId(uid);
        UpdateCartNumRequest cartNumRequest = new UpdateCartNumRequest();
        cartNumRequest.setChecked(cartForm.getChecked());
        cartNumRequest.setItemId(cartForm.getProductId());
        cartNumRequest.setUserId(cartForm.getUserId());
        cartNumRequest.setNum(cartForm.getProductNum());

        UpdateCartNumResponse updateCartNum = cartService.updateCartNum(cartNumRequest);
        if (updateCartNum == null){
            return new ResponseUtil().setErrorMsg("服务器繁忙");
        }
        if (!updateCartNum.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(updateCartNum.getMsg());
        }
        return new ResponseUtil<>().setData("成功");
    }

    @DeleteMapping("/carts/{uid}/{pid}")
   // public ResponseData deleteCartItem(@PathVariable("uid")Long userId,@PathVariable("pid")Long pid, HttpServletRequest request) {
    public ResponseData deleteCartItem(@PathVariable("pid")Long pid, HttpServletRequest request) {
        // String token = CookieUtil.getCookieValue(request, "access_token");
        //
        // String uidStr = JwtTokenUtils.builder().token(token).build().freeJwt();
        // Long uid = Long.valueOf(uidStr);
        String userInfo = (String) request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());


        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setItemId(pid);
       // deleteCartItemRequest.setUserId(userId);
        deleteCartItemRequest.setUserId(uid);
        cartService.deleteCartItem(deleteCartItemRequest);

        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMessage("success");
        responseData.setResult("删除购物车中的商品成功");
        responseData.setSuccess(true);
        return responseData;
    }


    @RequestMapping(value = "/items/{uid}",method = RequestMethod.DELETE)
    public ResponseData deleteCheckedItem(@PathVariable("uid") Long userId){
        DeleteCheckedItemRequest deleteCheckedItemRequest=new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(userId);
        DeleteCheckedItemResposne deleteCheckedItemResposne= cartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(deleteCheckedItemResposne.getCode())) {
            // 业务方法调用成功
            return new ResponseUtil().setData("成功");
        }

        return new ResponseUtil().setErrorMsg("失败");

    }
}
